package com.haixiaoke.saas.workOrder.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.DesensitizeUtils;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.workOrder.domain.WorkOrderPerson;
import com.haixiaoke.saas.workOrder.mapper.WorkOrderPersonMapper;
import com.haixiaoke.saas.workOrder.service.IWorkOrderPersonService;
import com.haixiaoke.saas.user.domain.BizUserBusiness;
import com.haixiaoke.saas.user.service.IBizUserBusinessService;
import com.haixiaoke.saas.workOrder.domain.WorkOrder;
import com.haixiaoke.saas.workOrder.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 工单人员信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-09-02
 */
@Service
public class WorkOrderPersonServiceImpl implements IWorkOrderPersonService {
    @Autowired
    private WorkOrderPersonMapper workOrderPersonMapper;
    @Autowired
    private IBizUserBusinessService bizUserBusinessService;
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单人员信息
     *
     * @param workOrderPersonId 工单人员信息主键
     * @return 工单人员信息
     */
    @Override
    public WorkOrderPerson selectWorkOrderPersonByWorkOrderPersonId(Long workOrderPersonId) {
        return workOrderPersonMapper.selectWorkOrderPersonByWorkOrderPersonId(workOrderPersonId);
    }

    /**
     * 查询工单人员信息列表
     *
     * @param workOrderPerson 工单人员信息
     * @return 工单人员信息
     */
    @Override
    public List<WorkOrderPerson> selectWorkOrderPersonList(WorkOrderPerson workOrderPerson) {
        return workOrderPersonMapper.selectWorkOrderPersonList(workOrderPerson);
    }

    /**
     * 新增工单人员信息
     *
     * @param workOrderPerson 工单人员信息
     * @return 结果
     */
    @Override
    public int insertWorkOrderPerson(WorkOrderPerson workOrderPerson) {
        workOrderPerson.setCreateTime(DateUtils.getNowDate());
        return workOrderPersonMapper.addWorkOrderPerson(workOrderPerson);
    }

    /**
     * 修改工单人员信息
     *
     * @param workOrderPerson 工单人员信息
     * @return 结果
     */
    @Override
    public int updateWorkOrderPerson(WorkOrderPerson workOrderPerson) {
        workOrderPerson.setUpdateTime(DateUtils.getNowDate());
        return workOrderPersonMapper.updateWorkOrderPerson(workOrderPerson);
    }

    /**
     * 批量删除工单人员信息
     *
     * @param workOrderPersonIds 需要删除的工单人员信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderPersonByWorkOrderPersonIds(Long[] workOrderPersonIds) {
        WorkOrderPerson workOrderPerson = new WorkOrderPerson();
        workOrderPerson.setWorkOrderPersonIds(workOrderPersonIds);
        List<WorkOrderPerson> workOrderPersonList = workOrderPersonMapper.selectWorkOrderPersonByWorkOrderPersonIds(workOrderPerson);
        if (workOrderPersonList.size() < workOrderPersonIds.length) {
            return 0;
        }
        List<WorkOrder> workOrderList = workOrderService.checkWorkOrder(workOrderPersonList);
        if (CollectionUtil.isNotEmpty(workOrderList)) {
            throw new ServiceException("存在工单未完成，请先完成工单再删除维修人员");
        }

        return workOrderPersonMapper.updateWorkOrderPersonByWorkOrderPersonIds(workOrderPersonIds);
    }

    /**
     * 删除工单人员信息信息
     *
     * @param workOrderPersonId 工单人员信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkOrderPersonByWorkOrderPersonId(Long workOrderPersonId) {
        return workOrderPersonMapper.deleteWorkOrderPersonByWorkOrderPersonId(workOrderPersonId);
    }

    @Override
    public int addWorkOrderPerson(WorkOrderPerson workOrderPerson) {
        BizUserBusiness busUser = bizUserBusinessService.selectBizUserBusinessByUserId(workOrderPerson.getBusUserId(), workOrderPerson.getTenantId());
        if (ObjectUtil.isEmpty(busUser)) {
            throw new ServiceException("企业用户不存在");
        }
        Tenant tenant = tenantService.selectDepByDepId(workOrderPerson.getTenantId());
        if (ObjectUtil.isEmpty(tenant)) {
            throw new ServiceException("租户不存在");
        }
        WorkOrderPerson userWorkOrderPerson =  workOrderPersonMapper.selectWorkOrderPersonByBusUserId(workOrderPerson.getBusUserId(), workOrderPerson.getTenantId());
        if (ObjectUtil.isNotEmpty(userWorkOrderPerson)) {
            throw new ServiceException("工单人员已存在");
        }
        workOrderPerson.setTenantName(tenant.getTenantName());
        workOrderPerson.setCreateTime(DateUtils.getNowDate());
        workOrderPerson.setWorkOrderPersonName(busUser.getUserName());
        workOrderPerson.setWorkOrderPersonPhone(busUser.getPhonenumber());
        return workOrderPersonMapper.addWorkOrderPerson(workOrderPerson);
    }

    @Override
    public List<WorkOrderPerson> selectWorkOrderPersonAndWorkOrder(WorkOrderPerson workOrderPerson) {
        return workOrderPersonMapper.selectWorkOrderPersonAndWorkOrder(workOrderPerson);
    }

    @Override
    public WorkOrderPerson selectWorkOrderPersonByBusUserId(Long busUserId, String tenantId) {
        return workOrderPersonMapper.selectWorkOrderPersonByBusUserId(busUserId, tenantId);
    }

    @Override
    public List<WorkOrderPerson> selectWorkOrderPersonAndPendingWorkOrder(WorkOrderPerson workOrderPerson) {
        List<WorkOrderPerson> workOrderPeople = workOrderPersonMapper.selectWorkOrderPersonAndPendingWorkOrder(workOrderPerson);
        if (workOrderPerson.getDesensitizeType()) {
            if (CollectionUtil.isNotEmpty(workOrderPeople)) {
                for (WorkOrderPerson orderPerson : workOrderPeople) {
                    orderPerson.setWorkOrderPersonName(DesensitizeUtils.desensitizeName(orderPerson.getWorkOrderPersonName()));
                    orderPerson.setWorkOrderPersonPhone(DesensitizeUtils.desensitizePhone(orderPerson.getWorkOrderPersonPhone()));
                }
            }
        }
        return workOrderPeople;
    }

    @Override
    public int updateWorkOrderPersonLatestOrderTime(String tenantId, Long busUserId) {
        return workOrderPersonMapper.updateWorkOrderPersonLatestOrderTime(tenantId, busUserId);
    }
}
